<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box1{width: 100px;height: 100px;background: red;position: absolute;left:0;top:0;}
        .box2{width: 100px;height: 100px;background: green;position: absolute;left:0;top:130px;}
        .line{width:1px;height: 500px;background: black;position: absolute;left:500px;top:0;}
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="line"></div>
</body>
<script>
    const obox1 = document.querySelector(".box1")
    const obox2 = document.querySelector(".box2")

    let t;
    
    let target = 500;

    obox1.onclick = function(){
        move(obox2);
    }


    function move(ele){
        clearInterval(t);
        t = setInterval(() => {
            // 随着时间的变化，不断减少步长值
            // 在最大步长的基础之上，每次改变n分之一
            // 最大步长：目标 - 当前值
            let speed = Math.ceil((target - ele.offsetLeft) / 7);

            if(target === ele.offsetLeft){
                clearInterval(t);
                ele.style.left = target + "px";
            }else{
                ele.style.left = ele.offsetLeft + speed + "px";
            }
        }, 30);
    }

</script>
</html>